package com.eastjet.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.equipment.mapper.DocumentationMapper;
import com.eastjet.equipment.service.DocumentationService;
import com.eastjet.equipment.util.RedisUtil;
import com.eastjet.maintenance.domain.DevicePmts;
import com.eastjet.maintenance.domain.Documentation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 杨博文
* @description 针对表【documentation(关联文档)】的数据库操作Service实现
* @createDate 2024-11-25 09:23:38
*/
@Service
public class DocumentationServiceImpl extends ServiceImpl<DocumentationMapper, Documentation>
    implements DocumentationService {

    @Autowired
    private DocumentationMapper documentationMapper;

    @Autowired
    private RedisUtil redisUtil;
    @Override
    public String document(Documentation documentation) {
        System.out.println("documentation:" + documentation.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库


            if (documentationMapper.insert(documentation) > 0) {
                // 定义Redis的键
                String redisKey = "documentList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition="";
                    List<Documentation> PmtstList = documentationMapper.selectList(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, PmtstList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "添加成功";
            } else {
                return "添加失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "添加失败";
        }

    }

    @Override
    public List<Documentation> doucumentQuery(String code) {
        String redisKey = "documentList:";
        List<Documentation> documentations;
        if(redisUtil.hasKey(redisKey)){
            redisUtil.del(redisKey);
            documentations= (List<Documentation>) redisUtil.get(redisKey);
            return documentations;

        }else{
            QueryWrapper qw=new QueryWrapper();
            qw.eq("code",code);
            documentations=documentationMapper.selectList(qw);
            redisUtil.set(redisKey,documentations);
            System.out.println("dou1:"+documentations);
            return documentations;

        }
    }

}




